package com.scau402.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.scau402.common.annotation.DataScope;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.DateUtils;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ClientCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scau402.system.mapper.WmsBasicClientMapper;
import com.scau402.system.domain.WmsBasicClient;
import com.scau402.system.service.IWmsBasicClientService;

/**
 * 客户资料Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
public class WmsBasicClientServiceImpl implements IWmsBasicClientService 
{
    @Autowired
    private WmsBasicClientMapper wmsBasicClientMapper;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询客户资料
     * 
     * @param id 客户资料主键
     * @return 客户资料
     */
    @Override
    public WmsBasicClient selectWmsBasicClientById(Long id)
    {
        return wmsBasicClientMapper.selectWmsBasicClientById(id);
    }

    /**
     * 查询客户资料列表
     * 
     * @param wmsBasicClient 客户资料
     * @return 客户资料
     */
    @Override
    @DataScope(tenantAlias = "t", userAlias = "u")
    public List<WmsBasicClient> selectWmsBasicClientList(WmsBasicClient wmsBasicClient)
    {
        wmsBasicClient.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return wmsBasicClientMapper.selectWmsBasicClientList(wmsBasicClient);
    }

    /**
     * 新增客户资料
     * 
     * @param wmsBasicClient 客户资料
     * @return 结果
     */
    @Override
    public int insertWmsBasicClient(WmsBasicClient wmsBasicClient)
    {
        wmsBasicClient.setCreateTime(DateUtils.getNowDate());
        wmsBasicClient.setTenantId(SecurityUtils.getLoginUser().getTenantId()); // 设置租户ID
        return wmsBasicClientMapper.insertWmsBasicClient(wmsBasicClient);
    }

    /**
     * 修改客户资料
     * 
     * @param wmsBasicClient 客户资料
     * @return 结果
     */
    @Override
    public int updateWmsBasicClient(WmsBasicClient wmsBasicClient)
    {
        wmsBasicClient.setUpdateTime(DateUtils.getNowDate());
        return wmsBasicClientMapper.updateWmsBasicClient(wmsBasicClient);
    }

    /**
     * 批量删除客户资料
     * 
     * @param ids 需要删除的客户资料主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicClientByIds(Long[] ids)
    {
        return wmsBasicClientMapper.deleteWmsBasicClientByIds(ids);
    }

    /**
     * 删除客户资料信息
     * 
     * @param id 客户资料主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicClientById(Long id)
    {
        return wmsBasicClientMapper.deleteWmsBasicClientById(id);
    }

    @Override
    public String getMaxCode(SysUser user) {
        Long tenantId = user.getTenantId();
        return wmsBasicClientMapper.getMaxCode(tenantId);
    }

    @Override
    public String getClientCode(String loginUserTenantCode) throws InterruptedException {
        ClientCodeUtils clientCodeUtils = new ClientCodeUtils();
        return clientCodeUtils.getNewCode(loginUserTenantCode);
    }

    @Override
    public boolean checkClientCodeUnique(WmsBasicClient wmsBasicClient) {
        if(wmsBasicClientMapper.checkClientCodeUnique(wmsBasicClient) > 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkClientNameUnique(WmsBasicClient wmsBasicClient) {
        if(wmsBasicClientMapper.checkClientNameUnique(wmsBasicClient) > 0) {
            return false;
        }
        return true;
    }

    @Override
    public String getNewCode(SysUser user) throws InterruptedException {
        // 获取数据库中最大的编码
        String maxCode = getMaxCode(user);

        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
        if(StringUtils.isNull(maxCode)) {
            maxCode = "0";
        }

        // 获取当前用户所属租户编码
        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
        String loginUserTenantCode = tenantService.selectSysTenantByTenantId(loginUserTenantId).getTenantCode();

        // 设置redis的当前编码
        ClientCodeUtils clientCodeUtils = new ClientCodeUtils();
        clientCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode);
        return clientCodeUtils.getNewCode(loginUserTenantCode);
    }


    @Override
    public int startOrStop(List<WmsBasicClient> wmsBasicClientList) {
        return wmsBasicClientMapper.startOrStopByIds(wmsBasicClientList);
    }
}
